package WorldCup2010.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnectionFactory {
	private Connection conn = null;
	private Statement stmt = null;
	private String jdbc_driver = "oracle.jdbc.driver.OracleDriver";
	private String jdbc_url = "jdbc:oracle:thin:@210.94.189.18:1521:DBLAB";
	private String id = "ST2004113959";
	private String password = "ST2004113959";
	private static ConnectionFactory connFactory; 
	
	private ConnectionFactory()
	{
		
	}
	public static ConnectionFactory getDefaultFactory()
	{
		if(connFactory == null)
		{
			connFactory = new ConnectionFactory();
		}
		return connFactory;
	}	
	public int createConnection()
	{
		try{
			Class.forName(jdbc_driver);
		}catch(ClassNotFoundException ex){
			ex.printStackTrace();
			return -1;
		}
		
		try{
			conn = DriverManager.getConnection(jdbc_url,id,password);
			stmt = conn.createStatement();			
		}catch(SQLException ex){
			System.out.println("connect Exception");
			ex.printStackTrace();
			return -2;
		}		
		
		return 0;
	}	
	public ResultSet executeQuery(String sql)
	{
		ResultSet rs = null;
		
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rs;
	}
	public void closeConnection()
	{
		if(stmt != null)
		{
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(conn != null)
		{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
